package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import entity.DoctorBaseInfo;
import entity.PatientBaseInfo;
import util.GetBaseInfo;
import util.GetRealIp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * @Author:
 * @Date: 2020-04-08
 * @Description: @PACKAGE:${PACKAGE_NAME}
 * @version: 1.0
 */
@WebServlet(name = "通过ID查找信息并返回",value = "/fillByIdNumberServlet")
public class FillByIdNumberServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String Patient_id = request.getParameter("Patient_id");
        String Doctor_id = request.getParameter("Doctor_id");
        String json="";
        if (Doctor_id==null){
            PatientBaseInfo patientBaseInfo = new PatientBaseInfo();
            try {
                patientBaseInfo= GetBaseInfo.GetPatientIdInfo(Patient_id);
                System.out.println(patientBaseInfo.toString());
                ObjectMapper mapper = new ObjectMapper();
                json = mapper.writeValueAsString(patientBaseInfo);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if (Patient_id==null) {
            DoctorBaseInfo doctorBaseInfo = new DoctorBaseInfo();
            try {
                doctorBaseInfo = GetBaseInfo.GetDoctorIdInfo(Doctor_id);
                System.out.println(doctorBaseInfo.toString());
                ObjectMapper mapper = new ObjectMapper();
                json = mapper.writeValueAsString(doctorBaseInfo);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }



        response.setContentType("text/html;charset=UTF-8");
        System.out.println(Patient_id);
        System.out.println(json.getClass().toString());
        System.out.println(GetRealIp.getIpAddress(request));

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(), json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}
